package com.android.infantschool.model.http;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtil {

    private OkHttpClient okHttpClient;
    private static volatile OkHttpUtil mInstance = null;

    private OkHttpUtil() {
        //日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)//响应时间，读取时间
                .readTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)//添加日志拦截器
                .build();
    }

    public static OkHttpUtil getmInstance() {
        if (mInstance == null) {
            synchronized (OkHttpUtil.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * GET同步请求
     *
     * @param url
     * @return
     */
    public String getSync(String url) {
        Response response = null;
        String string = null;
        try {
            //1:学什么都能new出一个对象来
            //Request就是请求的类
            Request request = new Request.Builder().url(url).build();
            //发送请求newCall方法
            Call call = okHttpClient.newCall(request);
            //通过call去处理给你响应Response
            response = call.execute();
            string = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //从相应体里面拿到数据
        return string;
    }

    /**
     * POST同步请求
     *
     * @param url
     * @param key
     * @param value
     * @return
     */
    public String postSync(String url, String key, String value) {
        //1:学什么都能new出一个对象来
        //创建请求体
        RequestBody requestBody = new FormBody.Builder()
                .add(key, value)//键值对的形式
                .build();
        //Request就是请求的类
        Request request = new Request.Builder().url(url).post(requestBody).build();
        //发送请求newCall方法
        Call call = okHttpClient.newCall(request);
        //通过call去处理给你响应Response
        Response response = null;
        String string = null;
        try {
            response = call.execute();
            string = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return string;
    }

    /**
     * get异步
     *
     * @param what
     * @param httpCallBack
     * @param httpUrl
     */
    public void getAsynHttp(final int what, final HttpCallBack httpCallBack, final String httpUrl) {
        Request.Builder requestBuilder = new Request.Builder().url(httpUrl);
        requestBuilder.method("GET", null);
        Request request = requestBuilder.build();
        Call mcall = okHttpClient.newCall(request);
        mcall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                httpCallBack.onFailure(what, e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                httpCallBack.onResponse(what, response.body().string());
            }
        });
    }

    /**
     * post异步
     *
     * @param what
     * @param httpCallBack
     * @param httpUrl
     * @param stringMap
     */
    public void postAsynHttp(final int what, final HttpCallBack httpCallBack, final String httpUrl, final Map<String, String> stringMap) {
        FormBody.Builder formBody = new FormBody.Builder();
        for (String key : stringMap.keySet()) {
            formBody.add(key, stringMap.get(key));
        }
        Request request = new Request.Builder()
                .url(httpUrl)
                .post(formBody.build())
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                httpCallBack.onFailure(what, e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                httpCallBack.onResponse(what, response.body().string());
            }
        });
    }

    public interface HttpCallBack {

        /**
         * 访问成功回调接口
         */
        void onResponse(int what, String response);

        /**
         * 访问成错误回调接口
         */
        void onFailure(int what, String error);
    }

}